package org.edu.modules.stu.base.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.edu.common.dto.ExcelPo;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.util.oConvertUtils;
import org.edu.common.utils.PoiUtils;
import org.edu.modules.stu.base.dto.VeStuChangeDto;
import org.edu.common.dto.AllDto;
import org.edu.modules.stu.base.entity.VeStuChange;
import org.edu.modules.stu.base.service.VeStuChangeService;
import org.edu.common.api.vo.Result;
import org.edu.common.base.PageDto;
import org.edu.common.dto.StatusDto;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.stu.common.entity.VeBaseAppUser;
import org.edu.modules.stu.common.service.VeBaseAppUserService;
import org.edu.modules.stu.stu.dto.VeStuLeaveDto;
import org.edu.modules.stu.stu.entity.VeStuLeave;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = {"学生学籍异动表"})
@RestController
@RequestMapping("/stu/veStuChange")
@Slf4j
public class VeStuChangeController {

    @Autowired
    private VeStuChangeService veStuChangeService;

    @Autowired
    private VeBaseAppUserService veBaseAppUserService;

    @ApiOperation("分页查询学生学籍异动表")
    @PostMapping("/page")
    public Result<?> page(@RequestBody PageDto<VeStuChange> page) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isEmpty(loginUser)) {
            return Result.error("请登录系统！");
        }
        //根据用户id查用户信息
        VeBaseAppUser appUser = veBaseAppUserService.getVeBaseAppUserById(loginUser.getId());
        IPage<VeStuChangeDto> pageDto = null;
        QueryWrapper<VeStuChange> queryWrapper = page.buildQueryWrapper(VeStuChange.class);
        if (appUser.getUserType().contains("2")) {
            queryWrapper.eq("code", appUser.getUserId());
            pageDto = veStuChangeService.page(page, queryWrapper);
        }else {
            pageDto = veStuChangeService.page(page, queryWrapper);
        }
        return Result.OK(pageDto);
    }

    /**
     * 查询所有
     */
    @ApiOperation("查询所有")
    @PostMapping("/all")
    public Result<?> all(@RequestBody AllDto<VeStuChange> page) {
        QueryWrapper<VeStuChange> queryWrapper = page.buildQueryWrapper(VeStuChange.class);
        List<VeStuChange> list = veStuChangeService.listByWrapper(queryWrapper);
        return Result.OK(list);
    }

    @ApiOperation("导出数据）")
    @PostMapping("/export")
    public Result<?> export(@RequestBody PageDto<VeStuChange> page, HttpServletResponse resp) {
        QueryWrapper<VeStuChange> queryWrapper = page.buildQueryWrapper(VeStuChange.class);
        List<ExcelPo> list = new ArrayList<>();
        list.add(new ExcelPo("学号", "code"));
        list.add(new ExcelPo("姓名", "name"));
        list.add(new ExcelPo("学生id", "stuId"));
        list.add(new ExcelPo("异动类别码;1=>'退学',2=>'休学',3=>'转专业',4=>'留级',5=>'转入',6=>'转出',", "ydlbm"));
        list.add(new ExcelPo("异动日期", "ydDate"));
        list.add(new ExcelPo("异动原因", "ydMsg"));
        list.add(new ExcelPo("审批日期", "spDate"));
        list.add(new ExcelPo("审批文号", "spwh"));
        list.add(new ExcelPo("经办人工号", "jbrgh"));
        list.add(new ExcelPo("异动来源学校码", "ydlyxxm"));
        list.add(new ExcelPo("异动去向学校码", "ydqxxm"));
        list.add(new ExcelPo("异动说明", "ydsm"));
        list.add(new ExcelPo("", "ofalId"));
        list.add(new ExcelPo("原专业码", "ospecId"));
        list.add(new ExcelPo("原班号", "objId"));
        list.add(new ExcelPo("原年级", "ogradeId"));
        list.add(new ExcelPo("院系", "falId"));
        list.add(new ExcelPo("专业码", "specId"));
        list.add(new ExcelPo("班级", "bjId"));
        list.add(new ExcelPo("年级", "gradeId"));
        list.add(new ExcelPo("异动恢复状态;1已恢复", "isRecover"));
        list.add(new ExcelPo("异动恢复操作人id", "recoverUserId"));
        list.add(new ExcelPo("异动恢复时间", "recoverTime"));
        PoiUtils.downloadFile(resp, veStuChangeService.page(page, queryWrapper).getRecords(), list);
        return Result.OK();
    }

    @AutoLog(value = "学生学籍异动表-分页列表查询")
    @ApiOperation(value = "学生学籍异动表-分页列表查询", notes = "学生学籍异动表-分页列表查询")
    @PostMapping(value = "/list")
    public Result<?> queryPageList(VeStuChange entity,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeStuChange> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        PageDto<VeStuChange> page = new PageDto<VeStuChange>(pageNo, pageSize);
        IPage<VeStuChange> pageList = veStuChangeService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "通过id查询")
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeStuChange dto = veStuChangeService.findDtoById(id);
        if (dto == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(dto);
    }

    @AutoLog(value = "添加")
    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeStuChangeDto dto) {
        veStuChangeService.insertOrUpdate(dto, true);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "编辑")
    @ApiOperation(value = "编辑", notes = "编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeStuChangeDto dto) {
        veStuChangeService.insertOrUpdate(dto, false);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "批量删除")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        veStuChangeService.delete(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    @AutoLog(value = "更新状态")
    @ApiOperation(value = "更新状态")
    @PostMapping(value = "/updateStatus")
    public Result<?> updateStatus(@RequestBody StatusDto statusDto) {
        UpdateWrapper<VeStuChange> updateWrapper = new UpdateWrapper<>();
        if (statusDto.getIds() != null && statusDto.getIds().size() > 0) {
            updateWrapper.set("status", statusDto.getStatus());
            updateWrapper.in("id", statusDto.getIds());
            veStuChangeService.update(updateWrapper);
            return Result.OK("批量更新成功!");
        } else {
            return Result.error("未选择!");
        }
    }


}
